//
//  SINAStudentMyViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/21.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINAStudentMyViewController.h"
#import "SINAStudentSettingViewController.h"
#import "SINAServicePariseViewController.h"
#import "SINASynthsizScoreViewController.h"
#import "SINACourseSignViewController.h"
#import "SINACourceScroViewController.h"
#import "SINAAskForLeaveViewController.h"
#import "SINAUserTitleDetaileViewController.h"
#import "SINAMainMsgViewController.h"
#import "SINAPersonInformationViewController.h"
#import "SINACircularBeadUtils.h"
#import "SINAStuApproveUserNameViewController.h"
#import "SINALoginViewController.h"
#import "SINANavLoginViewController.h"
#import "SINAControllerUtils.h"
#import "SINAAskListPageViewController.h"

@interface SINAStudentMyViewController ()

//用户头像
@property (weak, nonatomic) IBOutlet UIView *userTitleView;
@property (weak, nonatomic) IBOutlet UIImageView *userTitleImageView;

@property (weak, nonatomic) IBOutlet UIImageView *personInforBgImageView;

@property (weak, nonatomic) IBOutlet UIView *itemContentView;
//用户昵称
@property (weak, nonatomic) IBOutlet UILabel *nikeNameLabel;
//用户修改签名
@property (weak, nonatomic) IBOutlet UILabel *userSignLabel;

//请假申请
@property (weak, nonatomic) IBOutlet UIView *userAskForLeaveLabel;
//学科成绩
@property (weak, nonatomic) IBOutlet UIView *userCourseScoreLabel;
//课程签到
@property (weak, nonatomic) IBOutlet UIView *userCourseSignLabel;
//综合评分
@property (weak, nonatomic) IBOutlet UIView *userSynthsizScoreLabel;
//服务评分
@property (weak, nonatomic) IBOutlet UIView *userServiceAppraiseLabel;
//设置
@property (weak, nonatomic) IBOutlet UIView *userSettingLabel;
@end

@implementation SINAStudentMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"个人中心";
    
    self.view.backgroundColor = baseSelectColor;
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:0 target:self action:@selector(msgPageFunction)];
    //backItem.title = @"返回";
    backItem.image = [UIImage imageNamed:@"msg"];
    self.navigationItem.leftBarButtonItem = backItem;
    
    if (UI_IS_IPHONE6PLUS) {
        self.personInforBgImageView.image=[UIImage imageNamed:@"person_infor_pl_bg"];
    }
    
    
    UIBarButtonItem *myinforButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:0 target:self action:@selector(myInformationPageFunction)];
    //backItem.title = @"返回";
    myinforButtonItem.image = [UIImage imageNamed:@"input"];
    self.navigationItem.rightBarButtonItem = myinforButtonItem;
    
    [SINACircularBeadUtils makeCircularView:self.userTitleView];
    [SINACircularBeadUtils makeCircularBead:2 andView:self.itemContentView andIsSetBackground:NO];
    WeakSelf;
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userTitleView andWithBlick:^(UIView *view) {
        
        [weakSelf userTitleClickFunction];
    }];
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userAskForLeaveLabel andWithBlick:^(UIView *view) {
        
        if ([weakSelf checkApproveIdent]) {
            [weakSelf askForLeaveClickFunction];
        }
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userCourseScoreLabel andWithBlick:^(UIView *view) {
        if ([weakSelf checkApproveIdent]) {
            [weakSelf courseScoreClickFunction];
        }
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userCourseSignLabel andWithBlick:^(UIView *view) {
        if ([weakSelf checkApproveIdent]) {
            [weakSelf courseSignClickFunction];
        }
    }];
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userSynthsizScoreLabel andWithBlick:^(UIView *view) {
        if ([weakSelf checkApproveIdent]) {
            [weakSelf synthsizScoreClickFuction];
        }
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userServiceAppraiseLabel andWithBlick:^(UIView *view) {
        if ([weakSelf checkApproveIdent]) {
            [weakSelf serviceAppraiseClickFunction];
        }
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userSettingLabel andWithBlick:^(UIView *view) {
        
        [weakSelf settingClickFunction];
    }];
    
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.nikeNameLabel andWithBlick:^(UIView *view) {
        
        [weakSelf toPersonInformationPage];
    }];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.userSignLabel andWithBlick:^(UIView *view) {
        
        [weakSelf toPersonInformationPage];
    }];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jpushNotificationFunction:)name:notificationKeyNameStuMy object:nil];
    
}
- (void)jpushNotificationFunction:(NSNotification *)dic{
    
    
    WeakSelf;
    DLog(@"极光推送通知 个人页面------%@",dic.userInfo[@"pushMsgType"]);
    
    NSString *pushMsgType = dic.userInfo[@"pushMsgType"];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1200 * NSEC_PER_MSEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if (pushMsgType!=nil&& [pushMsgType isEqualToString:@"TECLEAVEMSG"]) {
            SINAAskListPageViewController *listViewController =[[SINAAskListPageViewController alloc]initWithNibName:@"SINAAskListPageViewController" bundle:nil];
            listViewController.hidesBottomBarWhenPushed=YES;
            [weakSelf.navigationController pushViewController:listViewController animated:YES];
        }
    });
    
    
}

-(void) msgPageFunction{
    //消息页面
    SINAMainMsgViewController
    *settingController = [[SINAMainMsgViewController alloc]initWithNibName:@"SINAMainMsgViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
-(void)myInformationPageFunction{
    //个人信息页面
    [self toPersonInformationPage];
}
//校验身份验证
-(BOOL) checkApproveIdent{
    WeakSelf;
    SINAUserModel *userModel = [[SINACommonUserHelper share]getUserModel];
    if (userModel==nil) {
        //重新登录
        SINALoginViewController *rootViewControll = [[SINALoginViewController alloc]init ];
        SINANavLoginViewController *homeController = [[SINANavLoginViewController alloc] initWithRootViewController:rootViewControll];
        [[SINAControllerUtils alloc]restoreRootViewController:homeController andWithWindow:self.view.window];
        return NO;
    } else {
        //校验
        NSString *universityid = userModel.universityid;
        if (universityid==nil||[universityid isEqualToString:@"(null)"]||universityid.length==0||[universityid integerValue]==0||[universityid integerValue]==-1) {
            WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"需要认证身份才可以查看" sureBtn:@"确定" cancleBtn:@"取消"];
            alertView.resultIndex = ^(NSInteger index) {
                SINAStuApproveUserNameViewController *stuApproveUserNameController = [[SINAStuApproveUserNameViewController alloc]initWithNibName:@"SINAStuApproveUserNameViewController" bundle:nil];
                stuApproveUserNameController.openType=1;
                stuApproveUserNameController.hidesBottomBarWhenPushed=YES;
                [weakSelf.navigationController pushViewController:stuApproveUserNameController animated:YES];
            };
            [alertView showXLAlertView];
            return NO;
        }
        return YES;
    }
}
-(void)viewWillLayoutSubviews{
    
}
-(void)viewWillAppear:(BOOL)animated{
    //    self.navigationController.navigationBarHidden = YES;
    
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
    //    if(IS_IPHONE_X){
    //        CGRect preFrameRect = self.view.frame;
    //        preFrameRect.origin.y=Height_StatusBar;
    //        self.view.frame =preFrameRect;
    //    }
    
    SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
    if (userModel!=nil) {
        //设置昵称
        NSString *userNikName = userModel.userNike;
        if (userNikName==nil||[userNikName isEqualToString:@""]) {
            NSString *phone = userModel.userPhone;
            if (phone==nil||[phone isEqualToString:@""]) {
                phone=@"设置昵称";
            }else{
                phone=@"设置昵称";
            }
            userNikName = phone;
        }
        self.nikeNameLabel.text=userNikName;
        //个性签名
        NSString *sign =userModel.sign;
        if (sign==nil||sign.length==0||[sign isEqualToString:@"(null)"]) {
            sign=@"这个世界很美，不仿写个签名吧";
        }
        self.userSignLabel.text = sign;
        //头像
        NSString *headerImageUrl = userModel.userTitleImageUrl;
        if (headerImageUrl!=nil&&headerImageUrl.length>1) {
            NSURL *imageUrl =[NSURL URLWithString:headerImageUrl];
            [self.userTitleImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"默认头像"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                DLog(@"加载头像完成");
            }];
        }
        
        //学院信息
        NSString *collectName = userModel.collegeName;
        NSString *universityName = userModel.universityName;
        if (collectName==nil||[collectName isEqualToString:@""]||universityName==nil||[universityName isEqualToString:@""]) {
            [self loadingInformationDetailFuntion:userModel.userUid];
        }
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//请假申请
- (void)askForLeaveClickFunction{
    SINAAskForLeaveViewController *settingController = [[SINAAskForLeaveViewController alloc]initWithNibName:@"SINAAskForLeaveViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//学科成绩
- (void)courseScoreClickFunction{
    SINACourceScroViewController *settingController = [[SINACourceScroViewController alloc]initWithNibName:@"SINACourceScroViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//课程签到
- (void)courseSignClickFunction {
    SINACourseSignViewController *settingController = [[SINACourseSignViewController alloc]initWithNibName:@"SINACourseSignViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//综合评分
- (void)synthsizScoreClickFuction {
    SINASynthsizScoreViewController *settingController = [[SINASynthsizScoreViewController alloc]initWithNibName:@"SINASynthsizScoreViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//服务评价
- (void)serviceAppraiseClickFunction{
    
    SINAServicePariseViewController *settingController = [[SINAServicePariseViewController alloc]initWithNibName:@"SINAServicePariseViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//设置
- (void)settingClickFunction {
    SINAStudentSettingViewController *settingController = [[SINAStudentSettingViewController alloc]initWithNibName:@"SINAStudentSettingViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}

//用户头像点击
- (void)userTitleClickFunction {
    SINAUserTitleDetaileViewController
    *settingController = [[SINAUserTitleDetaileViewController alloc]initWithNibName:@"SINAUserTitleDetaileViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//消息页面
- (IBAction)msgPageClickFunction:(UIButton *)sender {
    SINAMainMsgViewController
    *settingController = [[SINAMainMsgViewController alloc]initWithNibName:@"SINAMainMsgViewController" bundle:nil];
    settingController.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:settingController animated:YES];
}
//完善资料页面
- (IBAction)personInformation:(UIButton *)sender {
    if ([self checkApproveIdent]) {
        [self toPersonInformationPage];
    }
    
}

-(void) toPersonInformationPage{
    if ([self checkApproveIdent]) {
        SINAPersonInformationViewController
        *settingController = [[SINAPersonInformationViewController alloc]initWithNibName:@"SINAPersonInformationViewController" bundle:nil];
        settingController.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:settingController animated:YES];
    }
}

-(void)loadingInformationDetailFuntion:(NSString*)userUid{

    [self startLoading];
    WeakSelf;
    [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@?uuid=%@",baseUrl,userInformationUrl,userUid]  success:^(id responseData) {
        NSString *code = responseData[@"code"];
        NSString *universityname=@"无";
        NSString *realname=@"无";
        NSString *collname=@"无";
        NSString *classifyname=@"无";
        [weakSelf stopLoading];
        if ([code isEqualToString:@"1000"]) {
            NSDictionary *personInfromation = responseData[@"content"];
            universityname=personInfromation[@"universityname"];
            realname=personInfromation[@"realname"];
            collname=personInfromation[@"collname"];
            classifyname=personInfromation[@"classifyname"];
            SINAUserModel *userModel = [[SINACommonUserHelper share] getUserModel];
            userModel.collegeName=collname;
            userModel.universityName=universityname;
            [[SINACommonUserHelper share] saveUserModel:userModel];
            
        }else{
            NSString *message =responseData[@"message"];
            [YPXToastView showToastViewWithText:message andDuration:1 andParentView:self.view];
        }
        
       } failure:^(NSError *error, NSInteger errCode) {
           [weakSelf stopLoading];
        [YPXToastView showToastViewWithText:@"刷新个人信息失败" andDuration:1 andParentView:self.view];
    }];
}



@end
